package games;
import games.Player;
import games.HumanPlayer;
import games.Board;
import games.Dice;

import java.awt.Insets;
import java.util.Random;
import java.util.Vector;
import java.util.Scanner;
import java.util.Collections;



/*
 * Created by samil.can on 30.11.2014.
 */
public class Game {
	CircularDoublyLinkedList< Player> players;
    PlayerAdd playeradd;
    Board gameBoard;
    Dice dice;
    GameFrame rootframe;
    Game(){
        init();
        //playeradd = new PlayerAdd();
        //players = playeradd.getPlayers();
        rootframe = new GameFrame(this);
        /*playeradd.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        playeradd.setLocationRelativeTo(null);
        playeradd.setSize(300,300);
        playeradd.setVisible(true);*/
        rootframe.setVisible(true);

        //players = GameFrame.getPlayers();

        /*gameFrom gui = new gameFrom();
        gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        gui.setLocationRelativeTo(null);
        gui.setSize(300,300);
        gui.setVisible(true);*/
        
        Insets insets = rootframe.getInsets();
        rootframe.setSize(1200+insets.left+insets.right, 1000+insets.top+insets.bottom);
        
        

        //gui.setLocationRelativeTo(null);
        System.out.println("Game created.");
        rootframe.addLog("Game created.");
    }

    public void init(){
        players = new CircularDoublyLinkedList<Player>();
        gameBoard = new Board();
        dice = Dice.getDice();
        //addUsers();
        //randomizeUserOrder();
    }

    public static void main(String[] args){
        Game monopoly = new Game();
        //gameFrom gui = new gameFrom();
        //gameFrom gui = new gameFrom();

        //monopoly.init();

        //gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        //gui.setLocationRelativeTo(null);
        //gui.setSize(300,300);
        //gui.setVisible(true);
        //monopoly.gameStart();
        //monopoly.showResults();
    }

    void addUsers(){

    	String tempName;
    	Scanner in = new Scanner(System.in);

        boolean allPlayersAdded=true;
        System.out.println("To start the game please add min 4 Players");
        System.out.println("To end the adding player please press q");
        int n=0;
        while(allPlayersAdded){
            System.out.println("Please enter "+n+". player's name: ");
            tempName=in.nextLine();

            if(!tempName.equals("q")){
                Player tempPlayer = new HumanPlayer(gameBoard.board.elementAtDL(0), tempName);
                players.insertAtEnd(tempPlayer);
                n++;
            }
            else{
                if(players.getSize()>=4){
                    allPlayersAdded=false;
                    //return;
                }
                else{
                    System.out.println("Please add min 4 Players");
                    System.out.println("You need to add "+ (4-players.getSize()) + " Players");
                }

            }

        }

    }

    void randomizeUserOrder(){
        Random rand = new Random();
        int i = (rand.nextInt(players.getSize()));
        for (int j = 0; j < i/2; j++) {
        	players.swap(j, i);
        }
    }

    void gameStart(){
        //TODO we have ordered vector. players roll dice and move so on for every step. until all players bankruptcy except one
        
    	DLNode<Player> cur_player = players.elementAtDL(0);
        while(players.getSize()>1) //until become a monopoly
        {
            System.out.println(cur_player.getData().getPlayerName()+"'s turn.");
            rootframe.addLog(cur_player.getData().getPlayerName()+"'s turn.");
            int your_dice = dice.rollDice();
            System.out.println("Dice rolled: " + your_dice);
            rootframe.addLog("Dice rolled: " + your_dice);
            cur_player.getData().move(your_dice);

            if (cur_player.getData().getBankruptcy()){
                players.remove(cur_player);
            }

            cur_player=cur_player.getNextNode();

            if (cur_player.getData().isInJail()){
                cur_player.getData().setInjail(false);
                cur_player=cur_player.getNextNode();
            }
        }
    }

    void showResults(){
        //TODO game ended print results. 
    }


}


